﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class ManagePatrons : System.Web.UI.UserControl
{
    string lib;

    protected void Page_Load(object sender, EventArgs e)
    {
        lblConfirm.Text = "";
        lib = Request.QueryString["id"].ToString();
    }

    // Binds data to GridView1 when find person button is clicked
    protected void btnFindPerson_Click(object sender, EventArgs e)
    {
        //GridView1.DataSourceID = "SqlDataSource2";
        GridView1.DataBind();

    }

    //catches error when details view is updated and displays save confirmation message
    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            lblConfirm.Text = "A database error has occured. <br /><br /> Message: " + e.Exception.Message;
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;
        }
        else if (e.AffectedRows == 0)
            lblConfirm.Text = "Another user may have updated that record. <br /> Please try again.";
        else
            lblConfirm.Text = "Your record has been saved";
        GridView1.DataBind();
    }

    //catches error when a new record is inserted in details view and displays save confirmation message
    protected void DetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            //if (e.Exception.Message.ToString() == "Violation of PRIMARY KEY constraint 'PK_Person'. Cannot insert duplicate key in object 'dbo.PERSON'. Violation of PRIMARY KEY constraint 'PK_Employee'. Cannot insert duplicate key in object 'dbo.EMPLOYEE'. The statement has been terminated. The statement has been terminated.")
            //{
            lblConfirm.Text = "Your record has been saved.";
            e.ExceptionHandled = true;
            e.KeepInInsertMode = true;
            //}
        }
        else if (e.AffectedRows == 0)
            lblConfirm.Text = "Another user may have updated that record. <br /> Please try again.";
        else
            lblConfirm.Text = "Your record has been saved";
        GridView1.DataBind();
        lnkBtnFill.Visible = false;
        //DataTable dt = new DataTable();
        //DetailsView1.DataSource = dt;
        //DetailsView1.DataBind();
    }

    //-- Confirmation lable for searching for a person
    protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.AffectedRows < 1)
            lblNoResults.Text = "There are no results that match your search.";
        else
            lblNoResults.Text = "";
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        txtFindPerson.Text = "";
        lblNoResults.Text = "";
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        lnkBtnFill.Visible = true;
    }
    protected void lnkBtnFill_Click(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)DetailsView1.Rows[0].Cells[1].FindControl("txtId");
        txt.Text = "100013";
        TextBox txt2 = (TextBox)DetailsView1.Rows[1].Cells[1].FindControl("txtFirstName");
        txt2.Text = "Emily";
        TextBox txt3 = (TextBox)DetailsView1.Rows[2].Cells[1].FindControl("txtLastName");
        txt3.Text = "Jones";
        TextBox txt4 = (TextBox)DetailsView1.Rows[3].Cells[1].FindControl("txtAddress");
        txt4.Text = "123 Oak St";
        TextBox txt5 = (TextBox)DetailsView1.Rows[4].Cells[1].FindControl("txtCity");
        txt5.Text = "Monmouth";
        DropDownList ddl = (DropDownList)DetailsView1.Rows[5].Cells[1].FindControl("ddStates");
        ddl.Text = "OR";
        TextBox txt7 = (TextBox)DetailsView1.Rows[6].Cells[1].FindControl("txtZip");
        txt7.Text = "65432";
        TextBox txt8 = (TextBox)DetailsView1.Rows[7].Cells[1].FindControl("txtPhone");
        txt8.Text = "1231231233";
        TextBox txt9 = (TextBox)DetailsView1.Rows[8].Cells[1].FindControl("txtEmail");
        txt9.Text = "emily@abc.com";
        DropDownList ddl2 = (DropDownList)DetailsView1.Rows[9].Cells[1].FindControl("ddContact");
        //ddl2.SelectedValue = "Email";
        TextBox txt11 = (TextBox)DetailsView1.Rows[10].Cells[1].FindControl("txtPassword");
        txt11.Text = "Password1234";
        TextBox txt12 = (TextBox)DetailsView1.Rows[11].Cells[1].FindControl("txtCardExpires");
        txt12.Text = "01/01/2012";
        CheckBox chk = (CheckBox)DetailsView1.Rows[12].Cells[1].FindControl("cbInsertAdult");
        chk.Checked = true;
        CheckBox chk2 = (CheckBox)DetailsView1.Rows[13].Cells[1].FindControl("cbNewsLetter");
        chk2.Checked = true;
    }
}
